import 'package:flutter/material.dart';
import 'package:flutter_application_1/components/tap_mask/tap_mask.dart';

class Fab extends StatefulWidget {
  final Widget child;
  final double right;
  final double bottom;
  final double size;
  final Color color;
  final IconData icon;
  final Function? onTap;

  Fab({
    Key? key,
    required this.child,
    this.right = 30,
    this.bottom = 30,
    this.size = 50,
    this.color = Colors.blue,
    this.icon = Icons.add,
    this.onTap,
  }) : super(key: key);

  @override
  _FabState createState() => _FabState();
}

class _FabState extends State<Fab> {
  OverlayEntry? _fab;

  bool _darggable = false;

  Offset _position = Offset.zero;

  late FabButton _fabButton;

  _fabBuilder() {
    _fab?.remove();

    _fab = OverlayEntry(
      builder: (BuildContext context) {
        return Positioned(
          left: _darggable ? _position.dx : null,
          top: _darggable ? _position.dy : null,
          right: _darggable ? null : widget.right,
          bottom: _darggable ? null : widget.bottom,
          child: Draggable(
            child: _fabButton,
            childWhenDragging: SizedBox(),
            feedback: _fabButton,
            onDragEnd: (DraggableDetails details) {
              _darggable = true;
              _position = details.offset;
            },
          ),
        );
      },
    );

    Overlay.of(context)!.insert(_fab!);
  }

  @override
  void initState() {
    super.initState();

    _fabButton = FabButton(
      size: widget.size,
      color: widget.color,
      icon: widget.icon,
      onTap: widget.onTap,
    );

    WidgetsBinding.instance!.addPostFrameCallback((_) {
      _fabBuilder();
    });
  }

  @override
  void dispose() {
    _fab?.remove();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return widget.child;
  }
}

class FabButton extends StatelessWidget {
  final double size;
  final Color color;
  final IconData icon;
  final bool isShadow;
  final Function? onTap;

  FabButton({
    Key? key,
    this.size = 50,
    this.color = Colors.blue,
    this.icon = Icons.add,
    this.isShadow = true,
    this.onTap,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: size,
      height: size,
      decoration: BoxDecoration(
        color: color,
        borderRadius: BorderRadius.circular(size),
        boxShadow: isShadow
            ? <BoxShadow>[
                BoxShadow(
                  color: Color.fromRGBO(136, 136, 136, 0.3),
                  blurRadius: 20,
                  spreadRadius: 5,
                ),
              ]
            : [],
      ),
      child: TapMask(
        onTap: onTap,
        borderRadius: size,
        child: Center(
          child: Icon(
            icon,
            color: Colors.white,
          ),
        ),
      ),
    );
  }
}
